package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/18 13:38
 * @description：https://leetcode-cn.com/problems/magical-string/
 */
public class 神奇字符串 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.magicalString(6));
    }

    class Solution {
        public int magicalString(int n) {
            if (n == 0) return 0;
            StringBuilder builder = new StringBuilder("122");
            int ans = 1;
            int next = 2;
            for (int i = 2; i < n; i++) {
                if (builder.charAt(i) == '1') {
                    ans++;
                    if (builder.charAt(next) == '1') {
                        builder.append("2");
                    } else {
                        builder.append("1");
                    }
                } else {
                    if (builder.charAt(next) == '1') {
                        builder.append("22");
                    } else {
                        builder.append("11");
                    }
                    next++;
                }
                next++;
            }
            return ans;
        }
    }
}
